import AlgoPlus.TORAStockV3x as torastock


def loop_callback():
    global count
    global subscribe_list

    step = 100
    count += 1

    if count == 1:
        print(f'||Subseribe||,Count:{count}')
        torastock.subscribe(trader, subscribe_list)

    if count % step == 0:
        print(f'||Loop||,Count:{count}')
        if count == step * 3:
            torastock.unsubscribe(trader, subscribe_list)


if __name__ == '__main__':

    print(f"||Version||,Version:{torastock.getVersion()}")

    count = 0
    subscribe_list = ['000001', '600001', '300001']

    login_field = torastock.CAPLoginField()
    login_field.UserType = torastock.ENUM_UserType_NSIGHTStock
    login_field.UserID = ""
    login_field.Password = ""
    # NSight股票仿真交易环境（http://n-sight.com.cn）
    login_field.TraderFrontAddress = "tcp://210.14.72.11:4400"
    login_field.MdFrontAddress = "tcp://210.14.72.11:4402"

    trader = torastock.init(1, login_field, None, None, loop_callback)

    if trader is not None:
        print("||init||,Succeed")
        torastock.loop()
    else:
        error_id = torastock.getInitError()
        message = torastock.getMessageByErrorID(error_id)
        print(f"||init||,ErrorID:{error_id},Message:{message}")
